package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.AbstractCustomDialogFragment;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.tools.MoHuGlide;
import com.wgts.wgts.tools.QRCodeUtil;
import com.wgts.wgts.tools.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class QrCodeDialogFragment extends AbstractCustomDialogFragment {

    private static final String SHOP_KEEPER_QRCODE = "SHOPKEEPERQRCODE";

    private static final String SHOP_KEEPER_YQCODE = "SHOPKEEPERYQCODE";

    private static final String SHOPAVATAR = "shopAvatar";
    @BindView(R.id.img_qr_code)
    ImageView imgQrCode;
    @BindView(R.id.invitation_code)
    TextView invitationCode;
    @BindView(R.id.frame_img)
    FrameLayout frameImg;
    Unbinder unbinder;
    @BindView(R.id.linear_copy)
    LinearLayout linearCopy;
    @BindView(R.id.linear_save)
    LinearLayout linearSave;
    @BindView(R.id.invite_placeholder_text)
    TextView invitePlaceholderText;
    @BindView(R.id.uesr_head)
    ImageView uesrHead;
    Unbinder unbinder1;

    private String shopkeeperQRCode;

    private String shopkeeperYQCode;

    private String shopAvatar;

    private String shareId;

    public static final String SHAREID = "shareId";

    private String sharePath;

    @Override
    public View onCreateDialogView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.qr_code_dialog_fragment, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    public static QrCodeDialogFragment getInstance(String shopkeeperQRCode, String shopkeeperYQCode, String shopAvatar, String shareId) {
        QrCodeDialogFragment fragment = new QrCodeDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SHOP_KEEPER_QRCODE, shopkeeperQRCode);
        bundle.putString(SHOP_KEEPER_YQCODE, shopkeeperYQCode);
        bundle.putString(SHOPAVATAR, shopAvatar);
        bundle.putString(SHAREID, shareId);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initView() {
        Bundle bundle = getArguments();
        shopkeeperQRCode = bundle.getString(SHOP_KEEPER_QRCODE);
        shopkeeperYQCode = bundle.getString(SHOP_KEEPER_YQCODE);
        shopAvatar = bundle.getString(SHOPAVATAR);
        shareId = bundle.getString(SHAREID);

        invitationCode.setText(shopkeeperYQCode);


        sharePath = Tools.shareCodePath(shopkeeperQRCode, "invite-user", shareId);

        Resources res = getContext().getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.inivit_code_bg);
        imgQrCode.setImageBitmap(QRCodeUtil.addLogin(QRCodeUtil.createQRCodeBitmap(sharePath, 300, 300), bmp));
        MoHuGlide.getInstance().glideImg(getContext(), shopAvatar, uesrHead);

//        try {
//
//            Bitmap bitmap = MoHuGlide.getInstance().loadBitmap(getContext(), shopAvatar);
//            if(bitmap != null){
//                imgQrCode.setImageBitmap(QRCodeUtil.addLogin(QRCodeUtil.createQRCodeBitmap(sharePath, 130, 130), bitmap));
//            }else{
//                Resources res = getContext().getResources();
//                Bitmap bmp= BitmapFactory.decodeResource(res, R.mipmap.app_coin);
//                imgQrCode.setImageBitmap(QRCodeUtil.addLogin(QRCodeUtil.createQRCodeBitmap(sharePath, 130, 130), bmp));
//            }
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.linear_copy, R.id.linear_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_copy:
                if (sharePath != null) {
                    Tools.CopyToClipboard(getContext(), sharePath);
                }
                MoHuToast.showToast("复制成功");
                dismiss();
                break;
            case R.id.linear_save:
                Tools.KeepBitmap(frameImg, getContext());
                dismiss();
                break;
        }
    }
}
